$(function () {

    getReviewers({
        totalAmount: 0
    });

    // 初始化页面上的 datetimepicker
    insDatetimePicker('.is-date');

    layui.use(['form', 'layer'], function () {
        var form = layui.form(),
            layer = layui.layer;

        var initIndex = 1;

        // 添加明细项
        $('#add-detail-item').click(function () {
            // 获取明细数量
            var item_qutatity = ($(this).prev().find('tbody tr').length);
            var html = [
                '<tr>',
                    '<td class="valign-middle">' + (item_qutatity + 1) + '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + initIndex + '][part_no]" lay-verify="required" class="layui-input">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + initIndex + '][material_name]" lay-verify="required" class="layui-input">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + initIndex + '][specification_model]" lay-verify="required" class="layui-input">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + initIndex + '][unit_price]" lay-verify="required|money" class="layui-input listen unit_price">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + initIndex + '][quantity]" lay-verify="required|number" class="layui-input listen quantity">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + initIndex + '][unit]" lay-verify="required" class="layui-input">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + initIndex + '][amount]" readonly class="layui-input amount">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + initIndex + '][supplier]" class="layui-input">',
                    '</td>',
                    '<td class="valign-middle">',
                        '<input type="text" name="details[' + initIndex + '][comments]" class="layui-input">',
                    '</td>',
                    '<td>',
                        '<button type="button" class="btn btn-danger btn-sm remove-detail-item">删除</button>',
                    '</td>',
                '</tr>'
            ].join('');
            $('.cg-items-table tbody').append(html);
            initIndex++;
            // 添加明细后更新form组件避免出现无响应或渲染失败为空等现象
            form.render();
        });

        // 删除明细项
        $('.cg-items-table').on('click', '.remove-detail-item', function () {
            $(this).parent().parent().remove();
            var tr_len = $('.cg-items-table tbody').children().length;
            // 修正明细项删除后的序列错误
            $('.cg-items-table tbody tr').each(function (index) {
                $(this).find('td:first-child').text(index + 1);
            });

            amountCalculation();
        });

        $(document).on('input propertychange', '.cg-items-table tbody input.listen', function (e) {
            var tr = $(this).parent().parent();
            var unitPrice = parseFloat(tr.find('td:nth-child(5) input').val());
            var quantity = parseFloat(tr.find('td:nth-child(6) input').val());
            var amount = ((unitPrice ? unitPrice : 0) * (quantity ? quantity : 0));

            tr.find('td:nth-child(8) input').val(moneyFormat(amount));
            amountCalculation();
        });

        function amountCalculation() {
            var totalAmount = 0;
            // 填充行合计
            $('.cg-items-table tbody tr').each(function (index) {
                totalAmount += moneyToNumber($(this).find('.amount').val());
            });
            // 总金额赋值
            $('.cg-items-table tfoot tr td:last-child input').val(moneyFormat(totalAmount));

            throttle(getReviewers, null, approvalConfigs.delay, {
                totalAmount: totalAmount
            });
        }

    });
});